#include <stdio.h>
#include <stdint.h>
#include <webrtc/modules/audio_processing/agc/legacy/gain_control.h>
#include "audio_agc.h"
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif
void* audio_process_agc_create()
{
	void *handle_agc = WebRtcAgc_Create();
	return handle_agc;
}

int audio_process_agc_set_config(void *handle_agc,int32_t minLevel,int32_t maxLevel,
				int32_t agcMode,int samplerate,AgcConfig config)
{
	if(NULL == handle_agc)
		return -1;

	int nRet = 0;
	WebRtcAgcConfig tmpConfig;
	tmpConfig.targetLevelDbfs = config.targetLevelDbfs;
	tmpConfig.compressionGaindB = config. compressionGaindB;
	tmpConfig.limiterEnable = config.limiterEnable;

	nRet = WebRtcAgc_Init(handle_agc,minLevel,maxLevel,agcMode,samplerate);
	if(nRet != 0)
		return -1;

	nRet =  WebRtcAgc_set_config(handle_agc,tmpConfig);
	if(nRet != 0)
		return -1;

	return 0;
}

void audio_process_agc_get_config(void *handle_agc,AgcConfig* config)
{
		WebRtcAgcConfig tmpConfig;
		tmpConfig.targetLevelDbfs = config->targetLevelDbfs;
		tmpConfig.compressionGaindB = config->compressionGaindB;
		tmpConfig.limiterEnable = config->limiterEnable;

		WebRtcAgc_get_config(handle_agc,&tmpConfig);
}

int audio_process_agc_process(void* handle_agc,
				const int16_t* const* inNear,
				size_t num_bands,
				size_t samples,
				int16_t* const* out,
				int32_t inMicLevel,
				int32_t* outMicLevel,
				int16_t echo,
				uint8_t* saturationWarning)
{
	int nRet = 0;
	nRet = WebRtcAgc_Process(handle_agc, inNear, num_bands, samples, out, inMicLevel,
				outMicLevel, echo, saturationWarning);
	if(nRet != 0)
		return -1;

	return 0;
}

void audio_process_agc_free(void* handle_agc)
{
	WebRtcAgc_Free(handle_agc);
}

